package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.business.impl.BoatsSystemServiceImpl;
import com.ship.boats.domain.SpBoatsCanalAccess;
import com.ship.boats.domain.dto.QueryCanalMessageRq;
import com.ship.boats.domain.vo.QueryCanalMessageInfo;
import com.ship.boats.mapper.BoatsCanalAccessMapper;
import com.ship.boats.service.BoatsCanalAccessService;
import com.ship.common.core.constant.ShipApiConstants;
import com.ship.common.core.utils.StringUtils;
import com.ship.system.api.RemoteDispatchService;
import com.ship.system.api.domain.dispatch.SimpleShipInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <b>BoatsCanalAccessServiceImpl</b>
 *
 * @description: BoatsCanalAccessServiceImpl <br>
 * @date: 2024/4/10 13:44 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsCanalAccessServiceImpl extends ServiceImpl<BoatsCanalAccessMapper, SpBoatsCanalAccess> implements BoatsCanalAccessService {
    private static final Logger logger = LoggerFactory.getLogger(BoatsCanalAccessServiceImpl.class);

    @Autowired
    private BoatsSystemServiceImpl boatsSystemServiceImpl;

    @Resource
    private RemoteDispatchService remoteDispatchService;


    @Override
    public void addCanalAccessList() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date date = calendar.getTime();
        String formerlyDate = DateUtil.format(date, DatePattern.NORM_DATE_FORMATTER);
        Map<String,String> map = new HashMap<>();
        map.put("startDate",formerlyDate);
        map.put("endDate",formerlyDate);
        Object boatsUtil = boatsSystemServiceImpl.getBoatsUtil(map, ShipApiConstants.STATISTICS_PASU_PASSDETAIL);
        System.out.println("宝船网运河信息："+boatsUtil);
        List<SpBoatsCanalAccess> boatsList = JSONUtil.toList(JSONUtil.parseArray(boatsUtil), SpBoatsCanalAccess.class);
        if (CollectionUtil.isNotEmpty(boatsList)){
            boatsList.forEach(item -> {
                item.setEnable("1");
                item.setCreateTime(new Date());
                item.setUpdateTime(new Date());
            });
            this.saveBatch(boatsList);
        }
    }

    @Override
    public PageInfo<SpBoatsCanalAccess> queryCanalMessageList(Integer pageNum, Integer pageSize, String canalName) {
//        PageHelper.startPage(pageNum,pageSize);
        List<SpBoatsCanalAccess> canalAccessList = baseMapper.queryCanalMessageList(canalName);
        PageInfo<SpBoatsCanalAccess> pageInfo = null;
        if (StringUtils.isNotEmpty(canalName) && CollectionUtil.isNotEmpty(canalAccessList)){
            SpBoatsCanalAccess spBoatsCanalAccess = canalAccessList.get(0);
            List<SpBoatsCanalAccess> resultList = new ArrayList<>();
            resultList.add(spBoatsCanalAccess);
            pageInfo = new PageInfo<>(resultList);
        }else {
            pageInfo = new PageInfo<>(canalAccessList);
        }

        List<SimpleShipInfoVo> shipInfoVos = remoteDispatchService.getAllSimpleShipList();
        Map<String,List<SimpleShipInfoVo>> shipMap = new HashMap<>();
        if(shipInfoVos.size() > 0){
            shipMap = shipInfoVos.stream().collect(Collectors.groupingBy(SimpleShipInfoVo::getImo));
        }
        for (SpBoatsCanalAccess spBoatsCanalAccess : pageInfo.getList()) {
            if(spBoatsCanalAccess.getLngPassNum() > 0 && StringUtils.isNotBlank(spBoatsCanalAccess.getLngImos())){
                JSONArray imos = JSONArray.parseArray(spBoatsCanalAccess.getLngImos());
                List<String> shipNames = new LinkedList<>();
                for (Object imo : imos) {
                    if(shipMap.get(imo.toString()) != null){
                        shipNames.add(shipMap.get(imo).get(0).getShipName());
                    }else{
                        shipNames.add(imo.toString());
                    }
                }
                spBoatsCanalAccess.setShipNames(shipNames);
            }
        }
        return pageInfo;
    }

    @Override
    public PageInfo<QueryCanalMessageInfo> queryCanalMessageInfo(QueryCanalMessageRq rq) {
        if (StringUtils.isEmpty(rq.getStartDate())){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            Date date = calendar.getTime();
            String startDate = DateUtil.format(date, DatePattern.NORM_DATE_FORMATTER);
            rq.setStartDate(startDate);
            String endDate = DateUtil.format(new Date(), DatePattern.NORM_DATE_FORMATTER);
            rq.setEndDate(endDate);
        }
        PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
        List<QueryCanalMessageInfo> list = baseMapper.queryCanalMessageByCanalMessage(rq);
        PageInfo<QueryCanalMessageInfo> pageInfo = new PageInfo<>(list);

        List<SimpleShipInfoVo> shipInfoVos = remoteDispatchService.getAllSimpleShipList();
        Map<String,List<SimpleShipInfoVo>> shipMap = new HashMap<>();
        if(shipInfoVos.size() > 0){
            shipMap = shipInfoVos.stream().collect(Collectors.groupingBy(SimpleShipInfoVo::getImo));
        }
        for (QueryCanalMessageInfo spBoatsCanalAccess : pageInfo.getList()) {
            if(spBoatsCanalAccess.getLngPassNum() > 0 && StringUtils.isNotBlank(spBoatsCanalAccess.getLngImos())){
                JSONArray imos = JSONArray.parseArray(spBoatsCanalAccess.getLngImos());
                List<String> shipNames = new LinkedList<>();
                for (Object imo : imos) {
                    if(shipMap.get(imo.toString()) != null){
                        shipNames.add(shipMap.get(imo).get(0).getShipName());
                    }else{
                        shipNames.add(imo.toString());
                    }
                }
                spBoatsCanalAccess.setShipNames(shipNames);
            }
        }
            return pageInfo;
    }

    @Override
    public List<QueryCanalMessageInfo> exportCanalMessage(QueryCanalMessageRq rq) {
        List<QueryCanalMessageInfo> list = baseMapper.queryCanalMessageByCanalMessage(rq);
        return list;
    }


}
